﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;

namespace weighing
{
    /// <summary>
    /// BillSettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BillSettingWindow : GlowWindow
    {
        private bool isBindingValue = false;

        public BillSettingWindow()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }
        private void GlowWindow_Loaded(object sender, RoutedEventArgs e)
        {
        }
        private void GlowWindow_ContentRendered(object sender, EventArgs e)
        {
            SetPrinterItemSource();

            BindingValue();
        }

        private void GlowWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            App.Config.BillPrintConfig.InDesc = this.InTb.Text.Trim();
            App.Config.BillPrintConfig.OutDesc = this.OutTb.Text.Trim();
            LocalConfigDao.Instance.InsertOrUpdate(App.Config);
            Growl.Success("修改成功！", "main");
            this.Close();
        }
        /// <summary>
        /// 设置默认值
        /// </summary>
        private void BindingValue()
        {
            isBindingValue = true;
            this.InCalculateCb.IsChecked = App.Config.BillPrintConfig.InShowCalculate;
            this.OutCalculateCb.IsChecked = App.Config.BillPrintConfig.OutShowCalculate;

            this.EnablePrintInBillCb.IsChecked = App.Config.BillPrintConfig.EnablePrintInBill;
            this.EnablePrintOutBillCb.IsChecked = App.Config.BillPrintConfig.EnablePrintOutBill;

            this.InLimitTimesTb.Text = App.Config.BillPrintConfig.LimitInPrintTimes.ToString();
            this.OutLimitTimesTb.Text = App.Config.BillPrintConfig.LimitOutPrintTimes.ToString();

            this.InTb.Text = App.Config.BillPrintConfig.InDesc;
            this.OutTb.Text = App.Config.BillPrintConfig.OutDesc;

            this.AutoOpenWinCb.IsChecked = App.Config.BillPrintConfig.AutoOpenAutoPrintWindow;
            this.intervalTB.Text = App.Config.BillPrintConfig.AutoPrintInterval.ToString();

            this.EnableUnLoadingBillCb.IsChecked = App.Config.BillPrintConfig.EnableUnloadBill;
            this.AutoPrintUnloadBillCb.IsChecked = App.Config.BillPrintConfig.AutoPrintUnloadBill;
            this.unloadBillAngleTB.Text = App.Config.BillPrintConfig.UnLoadBillAngle.ToString();

            this.EnableLoadingBillCb.IsChecked = App.Config.BillPrintConfig.EnableLoadBill;
            this.AutoPrintLoadBillCb.IsChecked = App.Config.BillPrintConfig.AutoPrintLoadBill;
            this.loadBillAngleTB.Text = App.Config.BillPrintConfig.LoadBillAngle.ToString();

            if (App.Config.BillPrintConfig.AllUserDefaultPrinter)
            {
                this.AllUserDefaultRb.IsChecked = true;
            }
            else
            {
                this.SpecifiedPrinterRb.IsChecked = true;

                SetPrinterDefaultValue();
            }
            BindingPrintDateType();

 
            if (App.Config.BillPrintConfig.BillTitleType == DefaultBillTitleType.FixText)
            {
                this.fiexTitleTB.Visibility = Visibility.Visible;               
            }
            else
            {
                this.fiexTitleTB.Visibility = Visibility.Collapsed;
            }
            this.fiexTitleTB.Text = App.Config.BillPrintConfig.FiexTitle;
            this.defaultTitleCb.SelectedIndex = (int) App.Config.BillPrintConfig.BillTitleType;


           isBindingValue = false;
        }

        #region 采购 磅单显示货款结算信息 切换
        private void InCalculateCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.BillPrintConfig.InShowCalculate != this.InCalculateCb.IsChecked)
            {
                App.Config.BillPrintConfig.InShowCalculate = (bool)this.InCalculateCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
                CommonFunction.WeighingViewPricePanel();
            }
        }
        #endregion


        #region 销售 磅单显示货款结算信息 切换
        private void OutCalculateCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.BillPrintConfig.OutShowCalculate != this.OutCalculateCb.IsChecked)
            {
                App.Config.BillPrintConfig.OutShowCalculate = (bool)this.OutCalculateCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
                CommonFunction.WeighingViewPricePanel();
            }
        }


        #endregion

        private void OutLimitTimesTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.LimitOutPrintTimes = Convert.ToInt32(this.OutLimitTimesTb.Text);
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void InLimitTimesTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.LimitInPrintTimes = Convert.ToInt32(this.InLimitTimesTb.Text);
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void EnablePrintOutCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.BillPrintConfig.EnablePrintOutBill != this.EnablePrintOutBillCb.IsChecked)
            {
                App.Config.BillPrintConfig.EnablePrintOutBill = (bool)this.EnablePrintOutBillCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        private void EnablePrintInCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.BillPrintConfig.EnablePrintInBill != this.EnablePrintInBillCb.IsChecked)
            {
                App.Config.BillPrintConfig.EnablePrintInBill = (bool)this.EnablePrintInBillCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        #region 自动打开自动打印窗口
        private void AutoOpenWinCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoOpenAutoPrintWindow = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void AutoOpenWinCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoOpenAutoPrintWindow = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }
        #endregion

        //自动打单间隔
        private void SaveInteravlBtn_Click(object sender, RoutedEventArgs e)
        {
            int i = 0;
            try
            {
                i = Convert.ToInt32(this.intervalTB.Text);
            }
            catch (Exception)
            {
                CommonFunction.AlertInfo("只能输入整数");
                this.intervalTB.Focus();
                return;
            }
            App.Config.BillPrintConfig.AutoPrintInterval = i;
            int res = CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            if (res > 0)
            {
                CommonFunction.AlertInfo("保存成功");
            }
            else
            {
                CommonFunction.AlertInfo("保存失败");
            }
        }

        #region 卸货单

        private void EnableUnLoadingBillCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.EnableUnloadBill = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void EnableUnLoadingBillCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.EnableUnloadBill = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        //自动打印
        private void AutoPrintUnloadBillCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoPrintUnloadBill = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void AutoPrintUnloadBillCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoPrintUnloadBill = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }



        //Angle
        private void UnloadBillAngleTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isBindingValue) return;
            int angle = 0;
            try
            {
                angle = Convert.ToInt32(this.unloadBillAngleTB.Text);
            }
            catch (Exception ex)
            {
                this.unloadBillAngleTB.Text = "0";
            }
            App.Config.BillPrintConfig.UnLoadBillAngle = angle;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        #endregion

        #region 装货单

        private void EnableLoadingBillCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.EnableLoadBill = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void EnableLoadingBillCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.EnableLoadBill = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void AutoPrintLoadBillCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoPrintLoadBill = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void AutoPrintLoadBillCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AutoPrintLoadBill = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void LoadBillAngleTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isBindingValue) return;
            int angle = 0;
            try
            {
                angle = Convert.ToInt32(this.loadBillAngleTB.Text);
            }
            catch (Exception ex)
            {
                this.loadBillAngleTB.Text = "0";
            }
            App.Config.BillPrintConfig.LoadBillAngle = angle;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }
        #endregion

        #region 所有打印单据都使用默认打印机 切换
        private void AllUserDefaultRb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            App.Config.BillPrintConfig.AllUserDefaultPrinter = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }

        private void SpecifiedPrinterRb_Checked(object sender, RoutedEventArgs e)
        {
            if (isBindingValue) return;
            if (this.WeighingBillCb.ItemsSource == null)
            {
                SetPrinterItemSource();
            }

            App.Config.BillPrintConfig.AllUserDefaultPrinter = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }
        #endregion

        private void SetPrinterItemSource()
        {
            if (!App.Config.BillPrintConfig.AllUserDefaultPrinter)
            {
                var lps = new LocalPrintServer();
                var queues = lps.GetPrintQueues();

                List<string> its = new List<string>();

                foreach (var q in queues)
                {
                    its.Add(q.FullName);
                }
                if (its.Count == 0)
                {
                    Growl.Error("当前计算机没有任何安装任何打印机");
                }
                this.WeighingBillCb.ItemsSource = its;
                this.ExitSlipCb.ItemsSource = its;
                this.loadingCb.ItemsSource = its;
                this.unLoadingCb.ItemsSource = its;
            }
        }

        private void SetPrinterDefaultValue()
        {
            var its = this.WeighingBillCb.ItemsSource as List<string>;
            if (its == null) return;
            for (int i = 0; i < its.Count; i++)
            {
                if (its[i] == App.Config.BillPrintConfig.WeighingBillPrinterName)
                {
                    this.WeighingBillCb.SelectedIndex = i;
                    break;
                }
            }

            its = this.ExitSlipCb.ItemsSource as List<string>;
            for (int i = 0; i < its.Count; i++)
            {
                if (its[i] == App.Config.BillPrintConfig.ExitSlipPrinterName)
                {
                    this.ExitSlipCb.SelectedIndex = i;
                    break;
                }
            }

            its = this.loadingCb.ItemsSource as List<string>;
            for (int i = 0; i < its.Count; i++)
            {
                if (its[i] == App.Config.BillPrintConfig.LoadBillPrinterName)
                {
                    this.loadingCb.SelectedIndex = i;
                    break;
                }
            }

            its = this.unLoadingCb.ItemsSource as List<string>;
            for (int i = 0; i < its.Count; i++)
            {
                if (its[i] == App.Config.BillPrintConfig.UnLoadBillPrinterName)
                {
                    this.unLoadingCb.SelectedIndex = i;
                    break;
                }
            }
        }


        #region 选择打印机

        private void WeighingBillCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.WeighingBillCb.SelectedIndex < 0) return;
            var item = this.WeighingBillCb.SelectedItem;
            if (item == null) return;
            var name = item.ToString();
            if (App.Config.BillPrintConfig.WeighingBillPrinterName != name)
            {
                App.Config.BillPrintConfig.WeighingBillPrinterName = name;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        private void ExitSlipCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ExitSlipCb.SelectedIndex < 0) return;
            var item = this.ExitSlipCb.SelectedItem;
            if (item == null) return;
            var name = item.ToString();
            if (App.Config.BillPrintConfig.ExitSlipPrinterName != name)
            {
                App.Config.BillPrintConfig.ExitSlipPrinterName = name;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        private void LoadingCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.loadingCb.SelectedIndex < 0) return;
            var item = this.loadingCb.SelectedItem;
            if (item == null) return;
            var name = item.ToString();
            if (App.Config.BillPrintConfig.LoadBillPrinterName != name)
            {
                App.Config.BillPrintConfig.LoadBillPrinterName = name;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        private void UnLoadingCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.unLoadingCb.SelectedIndex < 0) return;
            var item = this.unLoadingCb.SelectedItem;
            if (item == null) return;
            var name = item.ToString();
            if (App.Config.BillPrintConfig.UnLoadBillPrinterName != name)
            {
                App.Config.BillPrintConfig.UnLoadBillPrinterName = name;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }

        #endregion

        #region 打印时间类型

        /// <summary>
        /// 绑定日期类型
        /// </summary>
        private void BindingPrintDateType()
        {
            int index = (int)App.Config.BillPrintConfig.PrintTimeType;
            this.printTimeCb.SelectedIndex = index;       
        }

        private void PrintTimeCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isBindingValue) return;
            if (this.printTimeCb.SelectedIndex < 0) return;
            var printType= (PrintTimeType) this.printTimeCb.SelectedIndex;
            if(printType != App.Config.BillPrintConfig.PrintTimeType)
            {
                App.Config.BillPrintConfig.PrintTimeType = printType;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }
        #endregion

        #region 抬头


        private void SaveTitleBtn_Click(object sender, RoutedEventArgs e)
        {
            var index = this.defaultTitleCb.SelectedIndex;
            var dt = (DefaultBillTitleType)index;
            App.Config.BillPrintConfig.BillTitleType = dt;
            App.Config.BillPrintConfig.FiexTitle = this.fiexTitleTB.Text;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));

            Growl.Info("保存成功");
        }
        private void FiexTitleTB_LostFocus(object sender, RoutedEventArgs e)
        {
            var index = this.defaultTitleCb.SelectedIndex;
            var dt = (DefaultBillTitleType)index;
            App.Config.BillPrintConfig.BillTitleType = dt;
            App.Config.BillPrintConfig.FiexTitle = this.fiexTitleTB.Text;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
        }
        private void DefaultTitleCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isBindingValue) return;
            if (this.defaultTitleCb.SelectedIndex < 0) return;
            var index = this.defaultTitleCb.SelectedIndex;
            if(index == (int)DefaultBillTitleType.FixText)
            {
                this.fiexTitleTB.Visibility = Visibility.Visible;
            }
            else
            {
                this.fiexTitleTB.Visibility = Visibility.Collapsed;
            }
            var dt = (DefaultBillTitleType)index;
            if (dt != App.Config.BillPrintConfig.BillTitleType)
            {
                App.Config.BillPrintConfig.BillTitleType = dt;
                App.Config.BillPrintConfig.FiexTitle = this.fiexTitleTB.Text;
                CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            }
        }
        #endregion

 
    }
}
